package GUI;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import sun.audio.AudioData;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
import sun.audio.ContinuousAudioDataStream;
import Base.Movie;
import Base.MovieCompilation;

/*
 *  This class makes the sounds in the background.
 *  @author Wayang Group Bremen
 *  @version 0.9
 *  @param story of type MovieCompilation
 */

public class SoundGui {
	public MovieCompilation story;
	public Thread mythreadSound;

	public AudioData data;
	public InputStream in = null;
	public ContinuousAudioDataStream cas;

	public SoundGui(MovieCompilation story) {
		this.story = story;
	}

	public void startLoopSound() {

		if(!story.compilation.isEmpty()) {
			Movie activeMovie = story.compilation.get(story.getActiveMovie());

			if(!activeMovie.SoundPath.equalsIgnoreCase("default no sound")) {
				//System.out.println("play sound:" +activeMovie.SoundPath);
				//String soundFile = figureName.concat(postfix);
				//System.out.println("Playback SoundFile : " + soundFile);
				//InputStream in = null;
				String bgPath = "Data//BGSounds//";
				try {
					in = new FileInputStream(bgPath.concat(activeMovie.SoundPath));
				} catch (FileNotFoundException e1) {
					System.out.println("Audiofile not found !");
					e1.printStackTrace();
				}
				// Create an AudioStream object from the input stream.
				AudioStream as2 = null;
				try {
					as2 = new AudioStream(in);
					data = as2.getData();

				} catch (IOException e1) {
					System.out.println("Audiostream not found !");
					e1.printStackTrace();
				}         
				// Use the static class member "player" from class AudioPlayer to play
				// clip.
				cas = new ContinuousAudioDataStream(data);
				AudioPlayer.player.start(cas);
			}
		}
	}

	public void stopSound() {
		AudioPlayer.player.stop(cas);
	}
}
